package net.huashitong.interview.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.core.filter.BaseFilter;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.interview.databean.BusinessSummary;
import net.huashitong.interview.entity.NeedSolveProblem;
import net.huashitong.interview.entity.ProblemSummary;
import net.huashitong.interview.filter.SummaryRequestModel;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class SummaryDao extends StringDao<ProblemSummary> {

    /**
     * 查询企业走访汇总情况
     * @param model
     * @return
     */
    public List<BusinessSummary> queryBusinessSummary(SummaryRequestModel model){
        StringBuffer sql = new StringBuffer("select " +
                "t.sys_id as id," +
                "t.fill_date as fillDate," +
                "t.fill_unit_id as fillUnitId," +
                "t.all_connect_business as allConnectBusiness," +
                "t.business_this_month as businessThisMonth," +
                "t.solved_problem as solvedProblem," +
                "t.working_problem as workingProblem," +
                "t.effect as effect," +
                "t.help_experience as helpExperience " +
                "from t_gis_interview_problem_summary t where 1=1");
        if(ObjectUtils.isNotBlank(model.getFillDate())){
            sql.append(" and t.fill_date like '"+model.getFillDate()+"%'");
        }
        if(ObjectUtils.isNotBlank(model.getFillUnitId())){
            sql.append(" and t.fill_unit_id = '"+model.getFillUnitId()+"'");
        }
        sql.append(" order by t.fill_date desc");
        SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(BusinessSummary.class));
        List<BusinessSummary> list  = (List<BusinessSummary>)query.list();
        return list;
    }

    public String getNewFillDate(){
        GenericQuery query = createQuery("obj");
        query.setOrder("fillDate",BaseFilter.DESC);
        ProblemSummary register = query.firstResult();
        return register.getFillDate();
    }

}
